package com.yao.longhan.ui.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.TextView;

import com.yao.longhan.BaseApplication;
import com.yao.longhan.R;
import com.yao.longhan.constant.Constant;
import com.yao.longhan.dao.DownloadInfoDao;
import com.yao.longhan.dao.StoryArticleDao;
import com.yao.longhan.eventbus.AddToShelfEvent;
import com.yao.longhan.eventbus.RxBus;
import com.yao.longhan.model.StoryArticle;
import com.yao.longhan.net.download.DownloadInfo;
import com.yao.longhan.net.download.DownloadManager;
import com.yao.longhan.ui.base.BaseActivity;
import com.yao.longhan.utils.SharedPreferencesUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by heyao on 2017/8/1.
 */

public class BookInfoActivity extends BaseActivity {

    @BindView(R.id.tv_book_info_title)
    TextView tvTitle;

    @BindView(R.id.tv_name)
    TextView tvName;

    @BindView(R.id.tv_author)
    TextView tvAuthor;

    @BindView(R.id.tv_chapter)
    TextView tvChapter;

    @BindView(R.id.tv_update_time)
    TextView tvUpdateTime;

    @BindView(R.id.tv_book_info_download)
    TextView tv_book_info_download;

    @BindView(R.id.info_add_to_shelf_tv)
    TextView info_add_to_shelf_tv;

    private StoryArticle mStoryArticle;
    private int mDownloadState;
    private StoryArticleDao mDao;

    @Override
    protected int inflateLayout() {
        return R.layout.activity_book_info;
    }

    @Override
    protected void initBefore() {
        mDao = BaseApplication.getInstance().getDaoSession().getStoryArticleDao();
    }

    @Override
    protected void initAfter() {
        Bundle bundle = getIntent().getExtras();
        mStoryArticle = bundle.getParcelable(Constant.BOOK_INFO);
        if (mStoryArticle == null) return;
        String artName = mStoryArticle.getArtName();
        tvTitle.setText(artName);
        tvName.setText(artName);
        String artAuth = mStoryArticle.getArtAuth();
        SpannableString authorString = new SpannableString(artAuth + " 著");
        authorString.setSpan(new ForegroundColorSpan(Color.parseColor("#ee3f3f")), 0, authorString.length() - 1,
                Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        tvAuthor.setText(authorString);

        tvChapter.setText(mStoryArticle.getNewChapterName());

        String lastUpdate = mStoryArticle.getLastUpdate();
        tvUpdateTime.setText("更新于" + lastUpdate);

        List<DownloadInfo> infos = DownloadInfo.DAO.queryBuilder().where(DownloadInfoDao.Properties.BookId.eq(mStoryArticle.getId())).list();
        if (infos != null && infos.size() > 0) {
            DownloadInfo info = infos.get(0);
            mDownloadState = info.getState();
            if (mDownloadState == DownloadInfo.FINISH) {
                tv_book_info_download.setText("已下载");
            } else if (mDownloadState == DownloadInfo.DOWNLOADING || mDownloadState == DownloadInfo.WAITING) {
                tv_book_info_download.setText("下载中...");
            } else {
                tv_book_info_download.setText("下载");
            }
        } else {
            tv_book_info_download.setText("下载");
        }

        List<StoryArticle> list = mDao.queryBuilder().where(StoryArticleDao.Properties.Id.eq(mStoryArticle.getId())).list();
        if (list != null && list.size() > 0) {
            info_add_to_shelf_tv.setEnabled(false);
            info_add_to_shelf_tv.setText("已加入书架");
        }
    }

    @OnClick(R.id.tv_start_read)
    public void tv_start_read_click(View view) {
        switch (view.getId()) {
            case R.id.tv_start_read:
                int readCount = SharedPreferencesUtil.getInstance().getInt(Constant.BOOK_SHELF_SORT_KEY, 0) + 1;
                SharedPreferencesUtil.getInstance().putInt(Constant.BOOK_SHELF_SORT_KEY, readCount);
                mStoryArticle.setReadCount(readCount);
//                mDao.update(mStoryArticle);
                RxBus.getDefault().post(new AddToShelfEvent());
                Bundle bundle = new Bundle();
                bundle.putParcelable(Constant.BOOK_INFO, mStoryArticle);
                toActivity(ReadActivity1.class, bundle);
                break;
        }
    }

    @OnClick(R.id.info_add_to_shelf_tv)
    public void info_add_to_shelf_tv_click(View view) {
        int readCount = SharedPreferencesUtil.getInstance().getInt(Constant.BOOK_SHELF_SORT_KEY, 0) + 1;
        SharedPreferencesUtil.getInstance().putInt(Constant.BOOK_SHELF_SORT_KEY, readCount);
        mStoryArticle.setReadCount(readCount);
        mDao.insertOrReplace(mStoryArticle);
        RxBus.getDefault().post(new AddToShelfEvent());
        info_add_to_shelf_tv.setEnabled(false);
        info_add_to_shelf_tv.setText("已加入书架");
    }

    @OnClick(R.id.tv_book_info_download)
    public void tv_book_info_download_click(View v) {
        if (mStoryArticle == null) return;
        if (mDownloadState == DownloadInfo.FINISH
                || mDownloadState == DownloadInfo.DOWNLOADING
                || mDownloadState == DownloadInfo.WAITING)
            return;
        DownloadInfo book = new DownloadInfo();
        book.setBookId(mStoryArticle.getId());
        book.setName(mStoryArticle.getArtName());
        book.setAuthor(mStoryArticle.getArtAuth());
        book.setStatus(mStoryArticle.getArtStat());
        book.setUrl("http://down.hunhun520.com/18/18109.txt");
        DownloadManager.download(book);
        tv_book_info_download.setText("下载中...");
    }
}
